package com.brillilab.service.core.topics.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.common.entity.PageVo;
import com.brillilab.dao.mapper.topics.TopicDirectionMapper;
import com.brillilab.dao.mapper.topics.TopicMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.topics.Topic;
import com.brillilab.domain.po.topics.TopicDirection;
import com.brillilab.domain.vo.topics.TopicDirectionAddVo;
import com.brillilab.domain.vo.topics.TopicDirectionEditVo;
import com.brillilab.service.core.experiments.IExperimentService;
import com.brillilab.service.core.plans.IPlanService;
import com.brillilab.service.core.plans.IPlanTargetGroupService;
import com.brillilab.service.core.topics.ITopicDirectionService;
import com.brillilab.service.core.topics.ITopicExperimentService;
import com.brillilab.service.core.topics.ITopicTargetService;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class TopicDirectionServiceImpl implements ITopicDirectionService {

	@Resource
	private TopicDirectionMapper topicDirectionMapper;
	@Resource
	private TopicMapper topicMapper;
	@Resource
	private ITopicTargetService topicTargetService;
	@Resource
	private ITopicExperimentService topicExperimentService;
	@Resource
	private IPlanService planService;
	@Resource
	private IPlanTargetGroupService planTargetGroupService;
	@Resource
	private IExperimentService experimentService;

	@Override
	public List<TopicDirection> getListByTopicId(List<Long> topicIds) {
		return topicDirectionMapper.selectList(new LambdaQueryWrapper<TopicDirection>()
				.in(TopicDirection::getTopicId, topicIds).eq(TopicDirection::getIsDelete, BoolEnum.FALSE.getValue()));
	}

	@Override
	public TopicDirection getById(Long id) {
		return topicDirectionMapper.selectById(id);
	}

	/** WuMenghao start */
	/**
	 * 新建研究内容
	 * 
	 * @author WuMenghao
	 * @param addVo
	 * @param labId
	 * @param labMemberId
	 * @return
	 */
	@Override
	public TopicDirection addTopicDirection(TopicDirectionAddVo addVo) {

		Integer count = topicDirectionMapper.selectCount(
				new LambdaQueryWrapper<TopicDirection>().eq(TopicDirection::getTopicId, addVo.getTopicId()));

		TopicDirection topicDirection = new TopicDirection();
		topicDirection.setLabId(addVo.getLabId());
		topicDirection.setTopicId(addVo.getTopicId());
		topicDirection.setName(addVo.getName().trim());
		topicDirection.setFigure(String.format("研究内容%s", count + 1));

		topicDirectionMapper.insert(topicDirection);

		return topicDirection;
	}

	/**
	 * 删除研究内容
	 * 
	 * @author WuMenghao
	 * @param topicDirectionId
	 * @param labId
	 * @param labMemberId
	 * @return
	 */
	@Override
	public void delete(TopicDirection direction) {
		Long topicDirectionId = direction.getId();
		TopicDirection lastOne = getLastOne(direction.getTopicId());
		if (lastOne != null && !lastOne.getId().equals(topicDirectionId)) {
			TopicDirection topicDirection = new TopicDirection();
			topicDirection.setId(topicDirectionId);
			topicDirection.setIsDelete(BoolEnum.TRUE.getValue());
			int i = topicDirectionMapper.updateById(topicDirection);
			Assert.isTrue(i == 1, "删除失败");
		} else {
			int b = topicDirectionMapper.deleteById(topicDirectionId);
			Assert.isTrue(b == 1, "删除失败");
		}

		topicTargetService.deleteByDirectionId(topicDirectionId);
		topicExperimentService.delete(topicDirectionId, null);
		planTargetGroupService.delete(null, topicDirectionId, null);
		planService.deleteByTopicDirectionId(topicDirectionId);
		experimentService.delete(topicDirectionId, null, null);
	}

	@SuppressWarnings("unchecked")
	private TopicDirection getLastOne(Long topicId) {
		LambdaQueryWrapper<TopicDirection> queryWrapper = new LambdaQueryWrapper<TopicDirection>();
		queryWrapper.eq(TopicDirection::getTopicId, topicId);
		queryWrapper.orderByDesc(TopicDirection::getId);
		List<TopicDirection> list = topicDirectionMapper.selectList(queryWrapper);
		return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
	}

	/**
	 * 编辑研究内容
	 * 
	 * @author WuMenghao
	 * @param editVo
	 * @param labId
	 * @param labMemberId
	 * @return
	 */
	@Override
	public boolean editTopicDirection(TopicDirectionEditVo editVo) {
		TopicDirection topicDirection = new TopicDirection();
		topicDirection.setId(editVo.getTopicDirectionId());
		topicDirection.setName(editVo.getName().trim());
		return topicDirectionMapper.updateById(topicDirection) >= 0;
	}

	/**
	 * 获取研究内容列表
	 * 
	 * @author WuMenghao
	 * @param pageNum
	 * @param pageSize
	 * @param topicId
	 * @param labId
	 * @return
	 */
	@Override
	public PageVo<TopicDirection> selectPage(Integer pageNum, Integer pageSize, Long topicId, Long labId) {
		Topic topic = topicMapper.selectById(topicId);
		Assert.notNull(topic, "课题不存在!");
		Assert.isTrue(topic.getLabId().equals(labId), "课题不属于该实验室!");

		PageHelper.startPage(pageNum, pageSize);
		List<TopicDirection> topicDirections = topicDirectionMapper.selectList(new LambdaQueryWrapper<TopicDirection>()
				.eq(TopicDirection::getTopicId, topicId).eq(TopicDirection::getIsDelete, BoolEnum.FALSE.getValue()));
		return new PageVo<>(topicDirections);
	}
	/** end */
}
